package com.codeplay.controller;

import com.codeplay.entity.Content;
import com.codeplay.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/contents")
@CrossOrigin(origins = "*")
public class ContentController {

    private final ContentService contentService;

    @Autowired
    public ContentController(ContentService contentService) {
        this.contentService = contentService;
    }

    @PostMapping
    public ResponseEntity<Content> createContent(@RequestBody Content content) {
        return ResponseEntity.ok(contentService.createContent(content));
    }

    @GetMapping("/{id}")
    public ResponseEntity<Content> getContentById(@PathVariable Long id) {
        return ResponseEntity.ok(contentService.getContentById(id));
    }

    @GetMapping("/chapter/{chapterId}")
    public ResponseEntity<List<Content>> getContentsByChapterId(@PathVariable Long chapterId) {
        return ResponseEntity.ok(contentService.getContentsByChapterId(chapterId));
    }

    @PutMapping("/{id}")
    public ResponseEntity<Content> updateContent(@PathVariable Long id, @RequestBody Content content) {
        content.setId(id);
        return ResponseEntity.ok(contentService.updateContent(content));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteContent(@PathVariable Long id) {
        contentService.deleteContent(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/chapter/{chapterId}/next/{currentOrderIndex}")
    public ResponseEntity<Content> getNextContent(
            @PathVariable Long chapterId, @PathVariable Integer currentOrderIndex) {
        return ResponseEntity.ok(contentService.getNextContent(chapterId, currentOrderIndex));
    }

    @GetMapping("/chapter/{chapterId}/previous/{currentOrderIndex}")
    public ResponseEntity<Content> getPreviousContent(
            @PathVariable Long chapterId, @PathVariable Integer currentOrderIndex) {
        return ResponseEntity.ok(contentService.getPreviousContent(chapterId, currentOrderIndex));
    }
} 